package com.yuan.zs.ui.account;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.yuan.base.framework.BaseFrameworkActivity;
import com.yuan.base.framework.bean.TipResult;
import com.yuan.base.framework.http.BaseHttpCallBack;
import com.yuan.base.framework.http.BaseHttpRequest;
import com.yuan.zs.R;
import com.yuan.zs.Urls;

import java.util.HashMap;

/**
 * 注册
 */
public class RegisterActivity extends BaseFrameworkActivity {

	private EditText et_user_code;
	private EditText et_user_pwd;
	private EditText et_user_pwd2;
	private EditText et_sms_code;
	private TextView tv_get_sms_code;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_register;
	}

	@Override
	protected void bindViews() {
		et_user_code = findViewById(R.id.et_user_code);
		et_user_pwd = findViewById(R.id.et_user_pwd);
		et_user_pwd2 = findViewById(R.id.et_user_pwd2);
		et_sms_code = findViewById(R.id.et_sms_code);
		tv_get_sms_code = findViewById(R.id.tv_get_sms_code);
		et_user_code.addTextChangedListener(new CodeWatcher());
		tv_get_sms_code.setOnClickListener(v -> getSmsCode());
		setOnClickListener(R.id.btn_submit, v -> submit());
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	private void submit() {
		String code = getStringByUI(et_user_code).replaceAll(" ", "");
		if (TextUtils.isEmpty(code)) {
			showToast("请输入手机号");
			return;
		}
		if (code.length() != 11) {
			showToast("请输入正确的手机号码");
			return;
		}
		if (TextUtils.isEmpty(getStringByUI(et_user_pwd))) {
			showToast("请输入密码");
			return;
		}
		if (TextUtils.isEmpty(getStringByUI(et_user_pwd2))) {
			showToast("请输入确认密码");
			return;
		}
		if (!getStringByUI(et_user_pwd2).equals(getStringByUI(et_user_pwd))) {
			showToast("两次密码输入不一致");
			return;
		}
		if (TextUtils.isEmpty(getStringByUI(et_sms_code))) {
			showToast("请输入验证码");
			return;
		}
		HashMap<String, Object> params = new HashMap<>();
		params.put("username", code);
		params.put("password", getStringByUI(et_user_pwd));
		params.put("apptype","1");
		params.put("sms", getStringByUI(et_sms_code));
		BaseHttpRequest.post(Urls.getUrl(Urls.register), params, new BaseHttpCallBack<TipResult>() {
			@Override
			public void onPreStart() {
				showProgressBar();
			}

			@Override
			public void onPost() {
				hideProgressBar();
			}

			@Override
			public void onSuccess(TipResult result) {
				if (result.isSuccess()) {
					showToast("注册成功");
					closeSelf();
				} else {
					showToast(result.getMessage());
				}
			}
		});
	}

	private void getSmsCode() {
		String code = getStringByUI(et_user_code).replaceAll(" ", "");
		if (TextUtils.isEmpty(code)) {
			showToast("请输入手机号");
			return;
		}
		if (code.length() != 11) {
			showToast("请输入正确的手机号码");
			return;
		}
		HashMap<String, Object> params = new HashMap<>();
		params.put("type", "sign-up");
		params.put("phone", code);
		BaseHttpRequest.get(Urls.getUrl(Urls.sendSms), params, new BaseHttpCallBack<TipResult>() {
			@Override
			public void onPreStart() {
				showProgressBar();
			}

			@Override
			public void onPost() {
				hideProgressBar();
			}

			@Override
			public void onSuccess(TipResult result) {
				if (result.isSuccess()) {
					tv_get_sms_code.setEnabled(false);
					timer.start();
					showToast(result.getMessage());
				} else {
					showToast(result.getMessage());
				}
			}
		});
	}

	@Override
	protected void onDestroy() {
		timer.cancel();
		super.onDestroy();
	}

	private CountDownTimer timer = new CountDownTimer(60000, 1000) {

		@Override
		public void onTick(long millisUntilFinished) {
			tv_get_sms_code.setText((millisUntilFinished / 1000) + "秒后可重发");
			tv_get_sms_code.setTextColor(getColorBase(R.color.gray));
		}

		@Override
		public void onFinish() {
			tv_get_sms_code.setEnabled(true);
			tv_get_sms_code.setText("获取验证码");
			tv_get_sms_code.setTextColor(getColorBase(R.color.common_text));
		}
	};

	private class CodeWatcher implements TextWatcher {
		int beforeTextLength = 0;
		int onTextLength = 0;
		boolean isChanged = false;

		int location = 0;// 记录光标的位置
		private char[] tempChar;
		private StringBuffer buffer = new StringBuffer();
		int konggeNumberB = 0;

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			onTextLength = s.length();
			buffer.append(s.toString());
			if (onTextLength == beforeTextLength || onTextLength <= 3 || isChanged) {
				isChanged = false;
				return;
			}
			isChanged = true;
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			beforeTextLength = s.length();
			if (buffer.length() > 0) {
				buffer.delete(0, buffer.length());
			}
			konggeNumberB = 0;
			for (int i = 0; i < s.length(); i++) {
				if (s.charAt(i) == ' ') {
					konggeNumberB++;
				}
			}
		}

		@Override
		public void afterTextChanged(Editable s) {
			if (isChanged) {
				location = et_user_code.getSelectionEnd();
				int index = 0;
				while (index < buffer.length()) {
					if (buffer.charAt(index) == ' ') {
						buffer.deleteCharAt(index);
					} else {
						index++;
					}
				}

				index = 0;
				int konggeNumberC = 0;
				while (index < buffer.length()) {
					if ((index == 3 || index == 8)) {
						buffer.insert(index, ' ');
						konggeNumberC++;
					}
					index++;
				}

				if (konggeNumberC > konggeNumberB) {
					location += (konggeNumberC - konggeNumberB);
				}

				tempChar = new char[buffer.length()];
				buffer.getChars(0, buffer.length(), tempChar, 0);
				String str = buffer.toString();
				if (location > str.length()) {
					location = str.length();
				} else if (location < 0) {
					location = 0;
				}

				et_user_code.setText(str);
				Editable etable = et_user_code.getText();
				Selection.setSelection(etable, location);
				isChanged = false;
			}
		}

	}
}

